package com.king.modules.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.annotation.MvcData;
import com.king.common.enums.ReadTypeEnum;
import com.king.common.utils.SecurityUtils;
import com.king.framework.base.controller.YyBaseController;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.framework.base.domain.vo.PageResult;
import com.king.modules.system.domain.SysMessageEntity;
import com.king.modules.system.service.SysMessageService;

/**
 * 消息 controller层
 * 
 * @author liusheng
 */
@RestController
@RequestMapping(value = "/system/message")
public class SysMessageController extends YyBaseController<SysMessageEntity> {

	Logger logger = LoggerFactory.getLogger(SysMessageController.class);

	@Autowired
	private SysMessageService messageService;

	@MvcData("获取")
	@GetMapping("/getAndRead/{uuid}")
	public AjaxResult getAndRead(@PathVariable("uuid") Long uuid) throws Exception {
		SysMessageEntity entity = messageService.findByPK(uuid);
		if (entity == null) {
			return error("没有数据");
		} else {
			if (entity.getReceiver() != null
					&& entity.getReceiver().longValue() == SecurityUtils.getUserId().longValue()
					&& entity.getIsnew() == ReadTypeEnum.UNREAD.getKey()) {
				messageService.setNoticeIsnew(entity);
			}
		}
		return success(entity);
	}

	@MvcData("批量标志为已读")
	@PostMapping(value = "/batchRead")
	public AjaxResult batchRead(@RequestBody(required = false) Long[] ids) throws Exception {
		if (ids == null || ids.length == 0) {
			return error("请选择记录");
		}
		messageService.batchRead(ids);
		return success("操作成功");
	}

	@MvcData("批量标志为已处理")
	@PostMapping(value = "/batchDeal")
	public AjaxResult batchDeal(@RequestBody(required = false) Long[] ids) throws Exception {
		if (ids == null || ids.length == 0) {
			return error("请选择记录");
		}
		messageService.batchDeal(ids);
		return success("操作成功");
	}
	
    @MvcData("查询")
    @PostMapping(value = "/queryBillMessage")
    public AjaxResult queryBillMessage(HttpServletRequest request, @RequestBody(required = false) SysMessageEntity form) throws Exception {
    	Map<String, Object> searchMap = new HashMap<String, Object>();
        PageResult<SysMessageEntity> datas = messageService.doQuery(searchMap);
        return success(datas);
    }
}
